import { login, logout } from '@/api/sys/oauth2'
import { getInfo } from '@/api/sys/user'
import { getToken, setToken, removeToken, getRefreshToken, setRefreshToken, removeRefreshToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'
import store from '../index'
const state = {
  info: '',
  status: '',
  code: '',
  token: getToken(),
  refresh_token: getRefreshToken(),
  name: '',
  avatar: '',
  introduction: '',
  roles: [],
  username: '',
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_REFRESH_TOKEN: (state, refresh_token) => {
    state.refresh_token = refresh_token
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_INFO: (state, info) => {
    state.info = info
  },
  SET_USERNAME: (state, username) => {
    state.username = username
  },
}

const actions = {
  setToken({ commit }, token) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', token)
      setToken(token)
      resolve()
    })
  },
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login(username.trim(), password)
        .then((response) => {
          const data = response.data
          commit('SET_TOKEN', data.access_token)
          setToken(response.data.access_token)
          commit('SET_REFRESH_TOKEN', data.refresh_token)
          commit('SET_USERNAME', username)
          setRefreshToken(response.data.refresh_token)
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then((response) => {
          if (!response.data) {
            // 由于mockjs 不支持自定义状态码只能这样hack
            reject('error')
          }
          const data = response.data.data

          if (data.roles && data.roles.length > 0) {
            // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', data.roles)
          } else {
            reject('getInfo: roles must be a non-null array !')
          }

          commit('SET_NAME', data.realname)
          commit('SET_INFO', data)
          commit('SET_AVATAR', data.portrait)
          commit('SET_INTRODUCTION', data.realname)
          resolve(data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // user logout
  logout({ commit, rootState }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_INFO', '')
          removeToken()
          removeRefreshToken()
          resetRouter()
          store.dispatch('permission/ResetPermissionsAndRoute')
          store.dispatch('tagsView/delAllViews')
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  },

  // dynamically modify permissions
  changeRoles({ commit, dispatch }, role) {
    return new Promise((resolve) => {
      const token = role + '-token'

      commit('SET_TOKEN', token)
      setToken(token)

      dispatch('getInfo').then(({ roles }) => {
        resetRouter()

        // generate accessible routes map based on roles
        dispatch('permission/generateRoutes', roles, { root: true }).then((accessRoutes) => {
          // dynamically add accessible routes
          router.addRoutes(accessRoutes)

          // reset visited views and cached views
          dispatch('tagsView/delAllViews', null, { root: true })

          resolve()
        })
      })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
